<template>
  <div class="home" ref="homeContainer">
    <div
      class="bj"
      :class="{ 'bj-other': !isHomePage }"
      v-if="!isAdvancedSearch"
    >
      <div class="navbar">
        <ul class="nav-menu">
          <li
            v-for="item in navItems"
            :key="item.featureName"
            @click="handleNavClick(item)"
            class="nav-item"
            :class="{ active: isActive(item) }"
          >
            <div style="position: relative">
              {{ item.featureName }}
              <div v-if="isActive(item)" class="active-line"></div>
            </div>
          </li>
          <li class="dropdown" v-if="subItems.length>0" @click.stop="toggleDropdown">
            <span>其他专题</span>
            <i
              class="dropdown-arrow el-icon-arrow-down"
              v-if="dropdownVisible"
            ></i>
            <i class="dropdown-arrow el-icon-arrow-up" v-else></i>
            <ul class="dropdown-menu" v-show="dropdownVisible">
              <li
                v-for="subItem in subItems"
                :key="subItem.featureName"
                @click="handleNavClick(subItem)"
              >
                <div class="zis"   :class="{ active: isActive(subItem) }" :title="subItem.featureName">
                  {{ subItem.featureName }}
                </div>
              </li>
            </ul>
          </li>
        </ul>
        <div class="user-area" @click="handleUserClick">
          <span>个人中心</span>
          <div class="block">
            <el-avatar :size="50" :src="circleUrl"></el-avatar>
          </div>
        </div>
      </div>
      <div class="title" :style="!isHomePage?'margin-top: 48px':''">
        <img
          style="width: 90px; height: 100px; margin-right: 23px"
          src="../assets//img/logo.png"
          alt=""
        />
        国防大学图书馆·QD研究专题资源库
      </div>
        <Search @openAdvancedSearch="openAdvancedSearch" />
        <NewestResources v-if="isHomePage" />
    </div>

    <transition name="fade">
      <AdvancedSearch v-if="isAdvancedSearch" />
    </transition>
    <transition name="fade">
      <div v-if="!isAdvancedSearch || isSearchResult">
        <router-view></router-view>
      </div>
    </transition>

    <div class="Bottom">
      <div class="xixin">
        <div style="display: flex">
          <span class="info-item">
            <i class="iconfont icon-dizhi"></i>
            地址：北京市某某区xxxxxx</span
          >
          <span class="info-item">
            <i class="iconfont icon-dianhua"></i>
            电话：18613849850</span
          >
          <span class="link-item">
            <i class="iconfont icon-kuaisurukou-"></i>
            后端入口：
            <div class="dian">【点击进入】</div>
          </span>
        </div>
        <div>
          <span class="copyright" style="margin-top: 12px">
            <i class="iconfont icon-copyright-circle-fil"> </i>
            版权所有：Copyright 2018-2023 www.xxx.cn</span
          >
        </div>
      </div>
    </div>
    <div class="back-top" v-if="showBackTop">
      <div @click="handleBackTop">
        <i class="iconfont icon-fanhuidingbu"></i>
      </div>
      <div style="width: 60px; height: 1px; background-color: #e4e4e4"></div>
      <div @click="handleOpenFeedback">
        <i class="iconfont icon-woyaofankui"></i>
      </div>
    </div>
    <div class="back-top1" v-else>
      <div @click="handleOpenFeedback">
        <i class="iconfont icon-woyaofankui"></i>
      </div>
    </div>
    <feedback-dialog ref="feedbackDialog"></feedback-dialog>
  </div>
</template>

<script>
import Search from "../components/manage/Search.vue";
import NewestResources from "../components/manage/NewestResources.vue";
import AdvancedSearch from "../components/manage/AdvancedSearch.vue";
import FeedbackDialog from "@/components/Feedback.vue";
import service from "@/service/service";
export default {
  name: "Manage",
  components: { Search, NewestResources, AdvancedSearch, FeedbackDialog },
  computed: {
    isHomePage() {
      return this.$route.path === "/home";
    },
    isAdvancedSearch() {
      return this.$route.query.advanced == "true";
    },
    isSearchResult() {
      return this.$route.query.searchz === "1";
    },
  },
  data() {
    return {
      navItems: [],
      subItems: [],
      dropdownVisible: false,
      circleUrl:
        "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png",
      showBackTop: false,
      homeContainer: null,
      currentPage: 1,
      pageSize: 100,
    };
  },
  mounted() {
    this.getHotTopic();

    this.homeContainer = this.$refs.homeContainer;
    if (!this.homeContainer) return;

    this.handleOutsideClick = (e) => {
      const dropdownEl = this.$el.querySelector(".dropdown");
      if (dropdownEl && !dropdownEl.contains(e.target)) {
        this.dropdownVisible = false;
      }
    };
    document.addEventListener("click", this.handleOutsideClick);

    this.handleScroll = () => {
      this.showBackTop = this.homeContainer.scrollTop > 500;
    };
    this.homeContainer.addEventListener("scroll", this.handleScroll);
  },
  methods: {
    async getHotTopic() {
      try {
        const res = await service({
          url: "/frontend/index/featureList",
          method: "post",
          data: {
            pageNo: this.currentPage,
            pageSize: this.pageSize,
          },
        });
        if (res.code === 200) {
          this.navItems = [
            { featureName: "首页", path: "/home" },
            ...res.data.list.slice(0, 4),
          ];
          this.subItems = res.data.list.slice(4, res.data.list.length);
        } else {
          this.$message.error(res.msg || "数据获取失败");
        }
      } catch (error) {
        this.$message.error(error || "数据获取失败");
      }
    },
    handleOpenFeedback() {
      this.$refs.feedbackDialog.openFeedbackDialog();
    },
    // 高级搜索
    openAdvancedSearch() {},
    isActive(row) {
      const currentPath = this.$route.path;
      if (row.featureName == "首页") {
        return currentPath == row.path;
      } else {
        const currentType = this.$route.query.type?.replace(/'/g, "");
        return currentType == row.featureName;
      }
    },
    handleNavClick(row) {
      console.log(row, "1111");
      if (row.featureName == "首页") {
        this.$router.push(row.path);
      } else {
        this.$router.push(`/SearchPage?type=${row.featureName}`);
      }
    },
    toggleDropdown() {
      this.dropdownVisible = !this.dropdownVisible;
    },
    handleSubNavClick(path) {
      this.$router.push(path);
      this.dropdownVisible = false;
    },
    handleUserClick() {
      // console.log("点击用户区域");
      this.$router.push("/PersonalCenter/MyCollection");
    },
    handleBackTop() {
      if (!this.homeContainer) return;
      this.homeContainer.scrollTo({
        top: 0,
        behavior: "smooth",
      });
    },
  },
  beforeDestroy() {
    // 移除原有事件监听
    document.removeEventListener("click", this.handleOutsideClick);
    if (this.homeContainer) {
      this.homeContainer.removeEventListener("scroll", this.handleScroll);
    }
  },
};
</script>

<style scoped lang="scss">
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.slide-enter-active,
.slide-leave-active {
  transition: all 0.3s ease;
}

.slide-enter-from {
  transform: translateX(-100%);
}

.slide-leave-to {
  transform: translateX(100%);
}

.copyright {
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  font-size: 18px;
  color: #ffffff;
}
.info-item {
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  font-size: 18px;
  color: #ffffff;
  margin-right: 42px;

  i {
    font-size: 18px;
    margin-left: 10px;
  }
}

.link-item {
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  font-size: 18px;
  color: #ffffff;
  display: flex;
  align-items: center;

  .dian {
    cursor: pointer;
  }
}

.home {
  height: 100vh;
  overflow: auto;
}

// .home::-webkit-scrollbar {
//   display: none;
// }

.bj {
  background-image: url(../assets/img/banner.png);
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  height: 980px;
  padding-bottom: 32px;
  &.bj-other {
    height: 474px;
    background-image: url(../assets/img/banner2.png);
  }
}

.navbar {
  width: 100%;
  height: 120px;
  // opacity: 0.8;
  // // background-color: #2060d7;
  background-color: rgba(32, 96, 215, 0.8);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 140px;
  box-sizing: border-box;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.nav-menu {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
}

.nav-item,
.dropdown {
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  font-size: 28px;
  color: #ffffff;
  padding-right: 90px;
  line-height: 40px;
  cursor: pointer;
  transition: background-color 0.3s;
  display: flex;
  align-items: center;
  transition: all 0.3s ease;
}
.dropdown-arrow {
  margin-left: 8px;
  font-size: 28px;
}
.nav-item:hover {
  font-weight: 600;
  transition: width 0.3s ease;
}

.nav-item.active {
  font-weight: 600;
}

.active-line {
  position: absolute;
  bottom: -13px;
  left: 0;
  width: 100%;
  height: 4px;
  border-radius: 2px;
  background-color: #fff;
}

.dropdown {
  position: relative;
}

.dropdown-menu {
  position: absolute;
  top: 60px;
  left: 0;
  min-width: 160px;
  background-color: #fff;
  list-style: none;
  margin: 0;
  padding: 10px 0;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.15);
  border-radius: 4px;
  z-index: 100;
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  font-size: 28px;
  color: #ffffff;
  width: 200px;

  li {
    padding: 8px 20px;

    color: #333;
    cursor: pointer;
    .zis {
      padding: 10px 15px;
      border-radius: 5px;

      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .zis.active{
      background-color: #e8f0fe;
      color: #327dff;
    }

    .zis:hover {
      background-color: #e8f0fe;
      color: #327dff;
    }
  }
}

.user-area {
  display: flex;
  align-items: center;
  cursor: pointer;

  span {
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 28px;
    color: #ffffff;
    padding-right: 36px;
  }
}

.title {
  margin-top: 67px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: AlimamaShuHeiTi, AlimamaShuHeiTi;
  font-size: 63px;
  color: #e51d28;
  -webkit-text-stroke: 2px #ffffff;
}

.back-top {
  position: fixed;
  right: 50px;
  bottom: 50px;
  cursor: pointer;
  z-index: 100;
  width: 68px;
  height: 136px;
  background: #ffffff;
  box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.18);
  border-radius: 8px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  font-size: 25px;
  color: #666666;
  padding: 0 4px;
  padding: 10px 4px;
  justify-content: space-evenly;
}
.back-top1 {
  position: fixed;
  right: 50px;
  bottom: 50px;
  cursor: pointer;
  z-index: 100;
  width: 68px;
  height: 60px;
  background: #ffffff;
  box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.18);
  border-radius: 8px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  font-size: 25px;
  color: #666666;
  padding: 0 4px;
  padding: 10px 4px;
  justify-content: space-evenly;
}

.Bottom {
  // margin-top: 52px;
  height: 120px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #2663d5;
  //   position: fixed;
  // bottom: 0;
  // width: 100%;

  .xixin {
    // min-width: 754px;
    height: 62px;
    // background: red;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
}
</style>